﻿//----------------------------------------------------------------
// <copyright file="BLL_PrivilegeInRoles.cs" >
//    Copyright (c) Wenzy , All rights reserved.
//    author:温正宇 E-Mail:wenzy@tom.com　MyBlog:wenzy.cnblogs.com
//    All rights reserved.
// </copyright>
//----------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using MemberShipDAL.DataObject.DataResourcesTableAdapters;
using MemberShipDAL;
using System.Collections;

namespace MemberServerBLL
{
    public class BLL_PrivilegeInRoles
    {
        /// <summary>
        /// 根据界面选择情况更新PrivilegeInRoles表里面的信息
        /// </summary>
        /// <param name="roleGuid"></param>
        /// <param name="privilegeGuid"></param>
        /// <param name="selected">Checkbox是否被选择</param>
        /// <returns></returns>
        public void UpdatePrivilegeInRoles(Guid roleGuid, Guid privilegeGuid, bool selected)
        {

            using (wmf_PrivilegeInRolesTableAdapter daPrivilegeInRoles = new wmf_PrivilegeInRolesTableAdapter())
            {
                bool isExists = false;
                isExists = PrivilegeInRolesIsExists(roleGuid, privilegeGuid);


                if (selected)
                {
                    //在此保存在PrivileveInRoles表里面
                    if (!isExists)
                    {
                        daPrivilegeInRoles.Insert(roleGuid, privilegeGuid, string.Empty, enum_PrivilegeEnable.Enable, DateTime.Now, DateTime.Now, DateTime.Now.AddYears(10), false);
                    }
                }
                else
                {
                    //把没有选择的删除掉
                    if (isExists)
                    {
                        daPrivilegeInRoles.DeleteQuery(roleGuid, privilegeGuid);

                    }

                }
            }
        }

        /// <summary>
        /// 根据RoleID和PrivilegeID来判断表PrivilegeInRoles是否存在此记录
        /// </summary>
        /// <param name="roleGuid"></param>
        /// <param name="privilegeGuid"></param>
        /// <returns></returns>
        static public bool PrivilegeInRolesIsExists(Guid roleGuid, Guid privilegeGuid)
        {
            ArrayList roles = GetRolesID(privilegeGuid);
            foreach (object role in roles)
            {
                if (role.ToString() == roleGuid.ToString())
                    return true;
            }
            return false;
        }

        /// <summary>
        /// 获得Privilege所在的Roles组
        /// </summary>
        /// <param name="privilegeGuid"></param>
        /// <returns></returns>
        static public ArrayList GetRolesID(Guid privilegeGuid)
        {
            MemberShipDAL.DataObject.DataResourcesTableAdapters.wmf_PrivilegeInRolesTableAdapter daPrivilegeInRoles = new wmf_PrivilegeInRolesTableAdapter();

            MemberShipDAL.DataObject.DataResources.wmf_PrivilegeInRolesDataTable dt = new MemberShipDAL.DataObject.DataResources.wmf_PrivilegeInRolesDataTable();
            daPrivilegeInRoles.FillPrivilegeInRoles(dt, privilegeGuid);

            ArrayList roles = new ArrayList();
            foreach (MemberShipDAL.DataObject.DataResources.wmf_PrivilegeInRolesRow row in dt.Rows)
            {
                if (!roles.Contains(row.RoleId))
                    roles.Add(row.RoleId);
            }
            return roles;


        }


    }
}
